package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/add")
public class UserAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //          2.接收前端的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //        3.将所有传递过来的数据保证到一个对象里面
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPassword(password);
        //        4.调用service层，将数据保存到数据库，
        UserService userService = new UserServiceImpl();
//        5.得到返回值：增加数据--
        ResultModel resultModel = userService.add(userModel);
        //        6.将结果转发到页面
        resp.getWriter().println(JSONObject.toJSON(resultModel));

    }
}
